/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-13 下午11:59 Created by dwzhang
 */
package com.jade.framework.base.util;

import java.util.AbstractList;

/**
 * 基于CachedList的PagingList，无法串行化.
 *
 * @author dwzhang
 * @version 1.0 13-4-13 下午11:59
 */
public class PagingCachedList<E> extends AbstractList<E> implements PagingList<E>
{
    private int offset;

    private CachedList<E> cachedList;

    private int length;

    public PagingCachedList(CachedList<E> cachedList, int off, int len)
    {
        this.cachedList = cachedList;
        this.offset = off;
        this.length = len;
    }

    /**
     * Returns the element at the specified position in this list.
     *
     * @param index index of element to return.
     * @return the element at the specified position in this list.
     * @throws IndexOutOfBoundsException if the given index is out of range
     *                                   (<tt>index &lt; 0 || index &gt;= size()</tt>).
     */
    public E get(int index)
    {
        return cachedList.get(offset + index);
    }

    /**
     * Returns the number of elements in this collection.  If the collection
     * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
     * <tt>Integer.MAX_VALUE</tt>.
     *
     * @return the number of elements in this collection.
     */
    public int size()
    {
        //尾页应该以totalCount - offset为准
        return getTotalCount() - offset > length ? length : getTotalCount() - offset;
    }

    /**
     * 返回数据集合总数
     *
     * @return 数据集合总数
     */
    public int getTotalCount()
    {
        return cachedList.size();
    }
}